package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private static UserDao userDao = new UserImpl();

    /**
     * 1.判断验证码
     * 2.判断用户信息
     *
     * @param userModel
     * @return
     */
    @Override
    public String login(UserModel userModel) {
//        验证码是否正确
        boolean booCode = userModel.getCode().equals(userModel.getSessionCode());
        if (!booCode) {
//            错误直接返回
            return "error-code";
        }
        //            查询用户
        boolean isHave = userDao.findByUsernameAndPassword(userModel) == null;//是否有用户
        if (!isHave) {
            return "success";
        }
        return "error";
    }

    @Override
    public int getCount(UserModel userModel) {
        return userDao.getCount(userModel);
    }

    @Override
    public String add(UserModel userModel) {
/**
 * 判断当前用户名是否在数据库存在？
 * 如果存在则提示用户以及存在
 * 否则直接增加
 *
 */
        boolean isHave = userDao.findByUsername(userModel.getUsername()) != null;
        if (isHave) {
            return "isHave";
        }
        return userDao.add(userModel) > 0 ? "success" : "error";
    }

    @Override
    public String update(UserModel userModel) {
        return userDao.update(userModel) > 0 ? "success" : "error";
    }

    @Override
    public String delete(UserModel userModel) {
        return userDao.delete(userModel) > 0 ? "success" : "error";
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        return userDao.findAll(userModel);
    }

    @Override
    public UserModel findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public String changeEnable(int id) {

        return userDao.changeEnable(id) > 0 ? "success" : "error";
    }
}
